/*
 * Adam Blackwell
 * ada2358@ccs.neu.edu
 */

import java.util.ArrayList;
import java.util.Arrays;

/***********************************************************************
 * Orbit class
 * 
 * Implements the 'orbit' command
 *
 * Signature:
 *  public static Orbit create ();
 *  public String name ();
 *  public boolean execute (String[] arguments);
 ***********************************************************************/


/**
 * Orbit:
 * @author Adam
 *
 */
public class Orbit extends Command {
  
  /**
   * Protected Constructor:
   */
  protected Orbit () {
    super("orbit");
  }
  
  /**
   * Default Creator:
   * @return
   */
  public static Orbit create () {
    return new Orbit();
  }
  
  /**
   * FindArtifactAroundPosition:
   * Looks at each adjacent Position if there is something in one it returns it.
   * @author Adam
   * @param c
   * @return
   */
  private static Option<Artifact> findArtifactAroundPosition (Coord c) {
    // Stored to make life easier, -- does this method break any rules?
    ArrayList<Integer> xPositions = 
      new ArrayList<Integer>(Arrays.asList(-1, 0, 1, 1, 1, 0, -1, -1));
    ArrayList<Integer> yPositions = 
      new ArrayList<Integer>(Arrays.asList(-1, -1, -1, 0, 1, 1, 1, 0));
    
    // Check through the adjacent elements:
    for (int i = 0; i < xPositions.size(); i++) {
      // Store Position, done to cut line length.
      Coord adj = c.add(Coord.create(xPositions.get(i), yPositions.get(i)));
      // If there's anything there return it.
      if(!SpaceGame.map().findArtifactByPosition(adj).isNone()){
        return SpaceGame.map().findArtifactByPosition(adj);
      }
    }
    // If we found nothing return a none...
    return Option.none();
  }
  
  /* (non-Javadoc)
   * @see Command#execute(java.lang.String[])
   * @author Adam
   */
  public boolean execute (String[] args) {
    
    Option<Artifact> adjacent = 
      Orbit.findArtifactAroundPosition(SpaceGame.me().position());
    
    if(adjacent.isNone()){
      System.out.println("can only orbit a near base or planet");
      return true;
    } else {
      SpaceGame.me().orbit(adjacent.valOf());
      return true;
    }
  }
}
